package com.onlocation.util;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.onlocation.R;

import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Author: Steve Carrell
 * Date: 5/12/12
 */
public class Logger {
    public static final String CLASS_NAME = Logger.class.getName();
    public static final String LOG_FILE = ResourceUtil.getString(R.string.app_name) +
            " " + new SimpleDateFormat("yyyyMMdd_HHmmss").format(
            Calendar.getInstance().getTime()) + ".log";
    public static final String TAG = "_" + ResourceUtil.getString(R.string.app_name) + "_";
    public static final int MAX_LOGS = 5;
    
    public static boolean writeToLog = true;

    public static void init() {
        // Create new log file; delete earliest dated if max reached then continue.
        if (writeToLog) {
            updateLogFiles();
        }
        i("Logging started.", CLASS_NAME);
    }

    public static int v(String msg, String className) {
        if (writeToLog) {
            toLogFile("VERBOSE", msg, className);
        }
        return Log.v(TAG, className + " " + msg);
    }

    public static int d(String msg, String className) {
        if (writeToLog) {
            toLogFile("DEBUG", msg, className);
        }
        return Log.d(TAG, className + " " + msg);
    }

    public static int i(String msg, String className) {
        if (writeToLog) {
            toLogFile("INFO", msg, className);
        }
        return Log.i(TAG, className + " " + msg);
    }

    public static int w(String msg, String className) {
        if (writeToLog) {
            toLogFile("WARN", msg, className);
        }
        return Log.w(TAG, className + " " + msg);
    }

    public static int e(String msg, String className) {
        if (writeToLog) {
            toLogFile("ERROR", msg, className);
        }
        return Log.e(TAG, className + " " + msg);
    }

    private static void toLogFile(String tag, String msg, String className) {
        try {
            FileOutputStream stream = Util.context.openFileOutput(LOG_FILE,
                    Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
            String fileMsg = tag + ": (" + className + ") " + msg + String.format("\r\n");
            stream.write(fileMsg.getBytes());
            stream.close();
        } catch (Exception e) {
            Log.e(TAG, "Unable to write to " + LOG_FILE);
        }
    }
    
    private static void updateLogFiles() {
        // files should be an array of /data/data/OnLocation/files/*.log
        File[] files = Util.context.getFilesDir().listFiles(new LogFilter());
        if (files.length >= MAX_LOGS) {
            // find oldest and delete it.
            File oldest = null;
            for (File file : files) {
                if (oldest == null || file.lastModified() <= oldest.lastModified()) {
                    oldest = file;
                }
            }
            if (oldest != null) {
                oldest.delete();
            }
        }
    }
    
    private static class LogFilter implements FileFilter {
        @Override
        public boolean accept(File pathname) {
            return pathname.getName().endsWith(".log");
        }
    }
}
